package tenantchannellogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type QrBankUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQrBankUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QrBankUpdateLogic {
	return &QrBankUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *QrBankUpdateLogic) QrBankUpdate(in *rpc.QrBankUpdateRequest) (*rpc.QrBankUpdateResponse, error) {
	var CommonResp rpc.CommonResp

	updates := map[string]interface{}{
		"min_amount":    in.MinAmount,
		"max_amount":    in.MaxAmount,
		"daily_times":   in.DailyTimes,
		"daily_amount":  in.DailyAmount,
		"remain_times":  in.RemainTimes,
		"remain_amount": in.RemainAmount,
		"vision_level":  in.VisionLevel,
		"is_display":    in.IsDisplay,
		"qr_code":       in.QrCode,
		"bank_nickname": in.BankNickname,
		"bank_name":     in.BankName,
		"sort":          in.Sort,
		"remark":        in.Remark,
	}

	err := model.NewTenantChannelQrBankModel(l.ctx, l.svcCtx.Db).UpdateQrBank(in.Id, updates)
	if err != nil {
		CommonResp.Code = xerrors.DBError
		CommonResp.Message = "Database operation error"
		return &rpc.QrBankUpdateResponse{
			CommonResp: &CommonResp,
		}, err
	}

	CommonResp.Message = "Success"
	return &rpc.QrBankUpdateResponse{
		CommonResp: &CommonResp,
	}, nil
}
